package cs.ui250.carnatic;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class RagaDisplay extends Activity {

	private SoundManager sm;
	private Raga currentRaga;
	private DBAdapter mDB;
	private ArrayList<String> songNamesArrayList;
	private Song[] currentSongs;
	private RagaDisplay mInstance = this;
	private TextView mNoResultsText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.raga_display);
		sm = SoundManager.getInstance(getApplicationContext());

		// Builds the Raga object for this operation
		mDB = DBAdapter.getInstance(getApplicationContext());
		currentRaga = mDB.getRaga(this.getIntent().getIntExtra("RAGA_ID_KEY",
				-1));

		// Sets ascending & descending scales
		TextView ragaNameView = (TextView) findViewById(R.id.raga_display_name);
		TextView arohanaView = (TextView) findViewById(R.id.raga_display_arohana_notes);
		TextView avarohanaView = (TextView) findViewById(R.id.raga_display_avarohana_notes);
		mNoResultsText = (TextView) findViewById(R.id.raga_display_empty_text);

		ragaNameView.setText(currentRaga.getName() + " (" + currentRaga.getID()
				+ ")");
		arohanaView.setText(currentRaga.printArohana());
		avarohanaView.setText(currentRaga.printAvarohana());

		// Sets Buttons for Raga playback
		Button playArohanaButton = (Button) findViewById(R.id.raga_display_arohana_button);
		Button playAvarohanaButton = (Button) findViewById(R.id.raga_display_avarohana_button);
		playArohanaButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				sm.playScale(currentRaga.printArohana());
			}
		});
		playAvarohanaButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				sm.playScale(currentRaga.printAvarohana());
			}
		});

		// Sets the ListView
		currentSongs = currentRaga.getSongArray();
		if (currentSongs != null) {
			mNoResultsText.setText("");
			songNamesArrayList = new ArrayList<String>(currentSongs.length);
			for (int i = 0; i < currentSongs.length; i++) {
				songNamesArrayList.add(currentSongs[i].getTitle());
			}

			// ListView code
			ListView songList = (ListView) findViewById(R.id.raga_display_song_list);
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
					R.layout.list_item, songNamesArrayList);
			songList.setAdapter(adapter);

			songList.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView parent, View v,
						int position, long id) {
					if (currentSongs != null) {
						SongDialog dialog = new SongDialog(mInstance,
								currentSongs[position]);
						dialog.show();
					}
				}
			});
		}
	}

	@Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		Window window = getWindow();
		window.setFormat(PixelFormat.RGBA_8888);
	}
}